package com.dou.ourpro.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dou.ourpro.pojo.Article;
import com.dou.ourpro.pojo.Users;
import com.dou.ourpro.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created with IntelliJ IDEA 2021.1 x64
 *
 * @Author: 窦义超
 * @Date: 2021/11/2021/11/11/14:53
 * @Description:
 */
@Controller
@RequestMapping("/art")
public class articleController {

    @Autowired
    ArticleService articleService;
    @GetMapping("/list")
    public String userList(Model model){
        List<Article> lists = articleService.list();
        model.addAttribute("empss",lists);
        return "article/article-list";
    }

    @GetMapping("/edit")
    public String artEdit(){
        return "article/art-edit";
    }
    @GetMapping("/artadd")
    public String artadd(){
        return "article/art-add";
    }


    @PostMapping("/add")
    public String add(Article article){
        boolean save = articleService.save(article);
        if(save){
        return "redirect:/art/list";}
        else return null;
    }

    @GetMapping("/artdelete")
    public String artdelete(@RequestParam int userId,@RequestParam int articleId){
            QueryWrapper<Article> queryWrapper=new QueryWrapper();
            queryWrapper.eq("article_id",articleId);
            queryWrapper.eq("user_id",userId);
            articleService.remove(queryWrapper);
        return "redirect:/art/list";
    }


    @PostMapping("/edit")
    public String artEdit1(Article article){
        QueryWrapper<Article> queryWrapper=new QueryWrapper();
        queryWrapper.eq("article_id",article.getArticleId());
        queryWrapper.eq("user_id",article.getUserId());
            articleService.update(article,queryWrapper);
        return "article/art-edit";
    }

    @GetMapping("/article")
    public String article(@RequestParam int userId, @RequestParam int artId,Model model){

        QueryWrapper<Article> queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("article_id",artId);
        Article one = articleService.getOne(queryWrapper);
        model.addAttribute("art",one);
        return "article/article";
    }

    @PostMapping("/Myedit")
    public String Myedit(Article article){
        System.out.println(article);
        QueryWrapper<Article> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("article_id",article.getArticleId());
        queryWrapper.eq("user_id",article.getUserId());
        article.update(queryWrapper);
        return "redirect:/art/list";
    }
}
